package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.base.adapter.RecyclerAdapter
import com.starsea.book.databinding.ItemImgBinding
import com.starsea.book.ui.newpage.entity.ImageSelEntity
import com.starsea.book.ui.newpage.utils.loadNormalImage
import com.starsea.book.ui.widget.recycler.ItemTouchCallback
import com.starsea.book.utils.gone
import com.starsea.book.utils.visible

/**
 * @author Lee
 * @createTime 2023-08-02 15 GMT+8
 * @desc :
 */
class ImageSelAdapter(
    var mContext: Context,
    var onChanged: ((value: ImageSelEntity, position: Int, type: Int) -> Unit)? = null,
) :
    BaseQuickAdapter<ImageSelEntity, ImageSelAdapter.MyViewHolder>() {

//    override fun getViewBinding(parent: ViewGroup): ItemImgBinding {
//        return ItemImgBinding.inflate(inflater, parent, false)
//    }

//    override fun registerListener(holder: ItemViewHolder, binding: ItemImgBinding) {
//
//        binding.ivDelete.setOnClickListener {
//            getItem(position = holder.layoutPosition).let {
//                onChanged!!.invoke(it!!, holder.layoutPosition, 3)
//            }
//        }
//        binding.ivImg.setOnClickListener {
//            getItem(position = holder.layoutPosition).let {
//                onChanged!!.invoke(it!!, holder.layoutPosition, 2)
//            }
//        }
//    }
//
//  override fun convert(
//    holder: ItemViewHolder,
//    binding: ItemImgBinding,
//    item: ImageSelEntity,
//    payloads: MutableList<Any>,
//  ) {
//    binding.run {
//      if (item.type == ImageSelEntity.KEY_NET) {
//        ivDelete.visible()
//        ivAdd.gone()
//        ivImg.loadNormalImage(mContext, item.path)
//      } else {
//        if (item.localMedia == null) {
//          ivDelete.gone()
//          ivAdd.visible()
//          ivImg.setImageResource(0)
//        } else {
//          ivDelete.visible()
//          ivAdd.gone()
//          ivImg.loadNormalImage(mContext, item.localMedia!!.path)
//        }
//      }
//
//    }
//
//  }


    override fun onBindViewHolder(holder: MyViewHolder, position: Int, item: ImageSelEntity?) {
        holder.binding.run {
            if (item!!.type == ImageSelEntity.KEY_NET) {
                ivDelete.visible()
                ivAdd.gone()
                ivImg.loadNormalImage(mContext, item.path)
            } else {
                if (item.localMedia == null) {
                    ivDelete.gone()
                    ivAdd.visible()
                    ivImg.setImageResource(0)
                } else {
                    ivDelete.visible()
                    ivAdd.gone()
                    ivImg.loadNormalImage(mContext, item.localMedia!!.path)
                }
            }

        }
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int,
    ): MyViewHolder {
        return MyViewHolder(parent = parent)
    }

    class MyViewHolder(
        parent: ViewGroup,
        val binding: ItemImgBinding = ItemImgBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)


}